//
//  AIInfoMsgViewCell.m
//  Mia
//
//  Created by Vill on 2023/12/21.
//

#import "AIInfoMsgViewCell.h"
#import "AIInsetLabel.h"

@interface AIInfoMsgViewCell ()
//{
//    NSString *_text;
//    NSTimer *_timer;
//    NSInteger _currentIndex;
//}
@property (nonatomic, strong) AIInsetLabel *contentLabel;
@property (nonatomic, strong) UIView *lblBgView;

@end

@implementation AIInfoMsgViewCell 

-(UIView *)lblBgView{
    if(!_lblBgView){
        _lblBgView = [UIView new];
        _lblBgView.backgroundColor = UIColor.whiteColor;
    }
    return _lblBgView;
}

- (AIInsetLabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [[AIInsetLabel alloc] initWithFrame:CGRectZero];
        _contentLabel.numberOfLines = 0;
        _contentLabel.textColor = [UIColor colorHex:@"#333333"];
        _contentLabel.font = [UIFont fontWithWeight:MTBold size:PTW(16)];
        _contentLabel.backgroundColor = UIColor.clearColor;
    }
    return _contentLabel;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 创建UILabel并设置相关属性
        [self.contentView addSubview:self.lblBgView];
        [self.contentView addSubview:self.contentLabel];
        self.contentView.backgroundColor = UIColor.clearColor;
        self.backgroundColor = UIColor.clearColor;
        
        [self.lblBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.mas_equalTo(self.contentLabel).mas_offset(-PTW(10));
            make.trailing.bottom.mas_equalTo(self.contentLabel).mas_offset(PTW(10));
        }];
//        NTFAdd(CloseInfoSetViewNotification, @selector(closeInfoSet:));
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    // 设置UILabel的frame
    CGSize size = [self.contentLabel.text boundingRectWithSize:CGSizeMake(self.contentView.bounds.size.width - PTW(20), MAXFLOAT)
                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      attributes:@{NSFontAttributeName:[UIFont fontWithWeight:MTBold size:PTW(16)]}
                                         context:nil].size;
    if (self.contentLabel.text.length == 0){
        self.contentLabel.frame = CGRectMake(PTW(0), PTW(0), size.width, size.height);
        self.lblBgView.frame = CGRectMake(PTW(0), PTW(0), size.width, size.height);
        self.height = 0;
    }else{
        self.contentLabel.frame = CGRectMake(PTW(10), PTW(10), size.width, size.height);
        self.lblBgView.frame = CGRectMake(PTW(0), PTW(0), size.width + PTW(20), size.height + PTW(20));
        self.height = size.height + PTW(20);
    }
    // 设置圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.lblBgView.bounds
                                                   byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight
                                                         cornerRadii:CGSizeMake(20, 20)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.lblBgView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.lblBgView.layer.mask = maskLayer;
}

- (void)setCharacters:(NSString *)text {
//    _text = text;
    self.contentLabel.text = text;
    // 更新布局
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
//    _currentIndex = 0;
//    self.contentLabel.text = @"";
//    // 创建定时器
//    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateText) userInfo:nil repeats:YES];
}


//- (void)updateText {
//    if (_currentIndex >= _text.length) {
//        // 所有文字都已经显示完毕，停止定时器
//        [_timer invalidate];
//        _timer = nil;
//        return;
//    }
//    // 逐个字添加到UILabel中
//    NSString *nextCharacter = [_text substringWithRange:NSMakeRange(_currentIndex, 1)];
//    self.contentLabel.text = [self.contentLabel.text stringByAppendingString:nextCharacter];
//    _currentIndex++;
//    // 更新布局
//    [self setNeedsLayout];
//    [self layoutIfNeeded];
//}
//
//#pragma mark ------------ 关闭view ---------------
//-(void)closeInfoSet:(NSNotification *)noti{
//    if (_timer){
//        [_timer invalidate];
//        _timer = nil;
//        
//    }
//}

@end
